//
//  YHOptionTitleTableViewCell.m
//  ScriptKill
//
//  Created by 林宁宁 on 2021/4/28.
//  Copyright © 2021 优智汇. All rights reserved.
//

#import "YHOptionTitleTableViewCell.h"
#import "UIFont+YH.h"
#import "UIButton+YH.h"
#import "UIImageView+YHWebImage.h"

@interface YHOptionTitleTableViewCell()
@property (retain, nonatomic) UIStackView * rightView;
@property (retain, nonatomic) UIStackView * leftView;
@end

@implementation YHOptionTitleTableViewCell

-(void)yh_commonInit{
        
    WS(weakSelf)
    
    self.cellContent = [UIView new];
    [self.contentView addSubview:self.cellContent];
    [self.cellContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
    
    
    {
        UIStackView * leftView = [UIStackView new];
        self.leftView = leftView;
        leftView.axis = UILayoutConstraintAxisHorizontal;
        leftView.alignment = UIStackViewAlignmentCenter;
        leftView.spacing = 5;
        leftView.distribution = UIStackViewDistributionEqualSpacing;
//        leftView.baselineRelativeArrangement = YES;
//        leftView.layoutMarginsRelativeArrangement = YES;
        [self.cellContent addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.cellContent);
            make.left.equalTo(self.cellContent).offset(YHOptionLayout_LeftTitleMargin());
        }];
        
        self.leftImageView = [UIImageView new];
        [leftView addArrangedSubview:self.leftImageView];
        
        self.leftTitleView = [UILabel new];
        self.leftTitleView.backgroundColor = [UIColor clearColor];
        self.leftTitleView.textAlignment = NSTextAlignmentLeft;
        self.leftTitleView.font = [UIFont yh_pfOfSize:14];
        self.leftTitleView.textColor = [UIColor yh_h1];
//        self.leftTitleView.numberOfLines = 0;
        [leftView addArrangedSubview:self.leftTitleView];
 
        self.leftImageView.hidden = YES;
        
//        self.leftImageView.backgroundColor = [UIColor yh_randomColor];
//        self.leftTitleView.backgroundColor = [UIColor yh_randomColor];
    }
    
    self.cellOptionContentView = [UIView new];
    self.cellOptionContentView.backgroundColor = [UIColor clearColor];
    [self.cellContent addSubview:self.cellOptionContentView];
    [self.cellOptionContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cellContent).offset(YHOptionLayout_CenterContentMargin());
        make.right.equalTo(self.cellContent).offset(-YHOptionLayout_LeftTitleMargin());
        make.top.bottom.equalTo(self.cellContent);
    }];
    
    {
        UIStackView * rightView = [UIStackView new];
        self.rightView = rightView;
        rightView.axis = UILayoutConstraintAxisHorizontal;
        rightView.alignment = UIStackViewAlignmentCenter;
        rightView.spacing = 5;
        rightView.distribution = UIStackViewDistributionEqualSpacing;
        [self.cellOptionContentView addSubview:rightView];
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.right.equalTo(self.cellOptionContentView);
            make.left.greaterThanOrEqualTo(self.cellOptionContentView);
        }];
        
        self.rightTitleView = [UILabel new];
        self.rightTitleView.textColor = [UIColor yh_h1];
        self.rightTitleView.font = [UIFont yh_pfOfSize:12];
        self.rightTitleView.textAlignment = NSTextAlignmentRight;
        self.rightTitleView.numberOfLines = 0;
        [rightView addArrangedSubview:self.rightTitleView];
        
        self.rightSwitchView = [UISwitch new];
        [self.rightSwitchView addTarget:self action:@selector(rightSwitchAction:) forControlEvents:UIControlEventValueChanged];
        [rightView addArrangedSubview:self.rightSwitchView];
        [self.rightSwitchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(55, 30));
        }];
        
        self.rightImageView = [UIImageView new];
        [rightView addArrangedSubview:self.rightImageView];
        
        self.rightCleanView = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.rightCleanView setImage:[UIImage imageNamed:@"clean"] forState:(UIControlStateNormal)];
        [self.rightCleanView addActionHandler:^(UIButton *sender, NSInteger tag) {
            if(weakSelf.rightViewEvent){
                weakSelf.rightViewEvent(weakSelf.indexPath, YHOptionCellRightShowType_Clean, weakSelf, @"");
            }
        }];
        [rightView addArrangedSubview:self.rightCleanView];
        [self.rightCleanView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
        
        self.rightTitleView.hidden = YES;
        self.rightImageView.hidden = YES;
        self.rightSwitchView.hidden = YES;
        self.rightCleanView.hidden = YES;
        
//        rightView.backgroundColor = [UIColor blueColor];
//        self.rightTitleView.backgroundColor = [UIColor yh_randomColor];
//        self.rightImageView.backgroundColor = [UIColor yh_randomColor];
//        self.rightSwitchView.backgroundColor = [UIColor yh_randomColor];
//        self.cellContent.backgroundColor = [UIColor redColor];
//        self.cellOptionContentView.backgroundColor = [UIColor greenColor];
//        self.rightCleanView.backgroundColor = [UIColor yh_randomColor];
    }
    
    [self yh_optionInit];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self.cellOptionContentView bringSubviewToFront:self.rightView];
}

-(void)setIsFullCell:(BOOL)isFullCell{
    if(_isFullCell == isFullCell){
        return;
    }
    _isFullCell = isFullCell;
    
    if(isFullCell){
        self.leftTitleView.hidden = YES;
        [self.cellOptionContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cellContent).offset(YHOptionLayout_LeftTitleMargin());
            make.right.equalTo(self.cellContent).offset(-YHOptionLayout_LeftTitleMargin());
            make.top.bottom.equalTo(self.cellContent);
        }];
    }else{
        self.leftTitleView.hidden = NO;
        [self.cellOptionContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cellContent).offset(YHOptionLayout_CenterContentMargin());
            make.right.equalTo(self.cellContent).offset(-YHOptionLayout_LeftTitleMargin());
            make.top.bottom.equalTo(self.cellContent);
        }];
    }
}

-(void)setIsFullNoneSpaceCell:(BOOL)isFullNoneSpaceCell{
    if(_isFullNoneSpaceCell == isFullNoneSpaceCell){
        return;
    }
    _isFullNoneSpaceCell = isFullNoneSpaceCell;
    self.leftTitleView.hidden = YES;
    [self.cellOptionContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.cellContent);
    }];
}

-(void)setContentHorizontalSpac:(CGFloat)contentHorizontalSpac{
    if(_contentHorizontalSpac == contentHorizontalSpac){
        return;
    }
    _contentHorizontalSpac = contentHorizontalSpac;
    [self.cellContent mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(contentHorizontalSpac);
        make.right.equalTo(self.contentView).offset(-contentHorizontalSpac);
        make.top.bottom.equalTo(self.contentView);
    }];
}

-(void)setRightShowType:(YHOptionCellRightShowType)rightShowType{

    _rightShowType = rightShowType;
    
    if(rightShowType & YHOptionCellRightShowType_Image){
        self.rightImageView.hidden = NO;
    }else{
        self.rightImageView.hidden = YES;
    }
    
    if (rightShowType & YHOptionCellRightShowType_Title){
        self.rightTitleView.hidden = NO;
    }else{
        self.rightTitleView.hidden = YES;
    }
    
    if (rightShowType & YHOptionCellRightShowType_Switch){
        self.rightSwitchView.hidden = NO;
    }else{
        self.rightSwitchView.hidden = YES;
    }
    
    if (rightShowType & YHOptionCellRightShowType_Clean){
        self.rightCleanView.hidden = NO;
    }else{
        self.rightCleanView.hidden = YES;
    }
}

-(void)setLeftShowType:(YHOptionCellLeftShowType)leftShowType{

    _leftShowType = leftShowType;
    
    if(leftShowType & YHOptionCellLeftShowType_Title){
        self.leftTitleView.hidden = NO;
    }else{
        self.leftTitleView.hidden = YES;
    }
    
    if (leftShowType & YHOptionCellLeftShowType_Image){
        self.leftImageView.hidden = NO;
    }else{
        self.leftImageView.hidden = YES;
    }
}

- (void)rightSwitchAction:(UISwitch *)sender{
    if(self.rightViewEvent){
        self.rightViewEvent(self.indexPath, YHOptionCellRightShowType_Switch, self, @(sender.on).stringValue);
    }
}

-(void)yh_optionInit{
    
}


-(void)updateLeftContentByItem:(YHOptionTableViewItem *)item{
    self.leftTitleView.textAlignment = item.layout.leftTitleAlignment;
    if(item.layout.leftTitleWidth > 0){
        [self.leftTitleView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(item.layout.leftTitleWidth);
        }];
    }else{
        [self.leftTitleView mas_remakeConstraints:^(MASConstraintMaker *make) {
        }];
    }
    self.leftTitleView.textColor = item.layout.leftTitleColor?:[UIColor yh_h1];
    self.leftTitleView.font = item.layout.leftTitleFont?:[UIFont yh_pfOfSize:14];
    
    if(item.layout.isFullNoneSpace){
        self.isFullNoneSpaceCell = YES;
    }else{
        self.isFullCell = item.layout.isFullCell;
    }
    
    self.leftShowType = item.layout.leftShowType;
    if(item.layout.leftShowType & YHOptionCellLeftShowType_Image){
        [self.leftImageView yh_setImage:item.leftContent.imageURL placeholderImage:item.layout.leftImagePlaceholder];
        if(item.layout.leftImageSize.width > 0){
            [self.leftImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(item.layout.leftImageSize);
            }];
            self.leftImageView.layer.cornerRadius = item.layout.leftImageCornerRadius;
            self.leftImageView.layer.masksToBounds = YES;
        }
        else if(self.leftImageView.image){
            [self.leftImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(self.leftImageView.image.size);
            }];
        }
        else{
            [self.leftImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(30, 30));
            }];
        }
    }
    if(item.layout.leftShowType & YHOptionCellLeftShowType_Title){
        self.leftTitleView.text = item.leftContent.title;
    }
//
//    self.leftView.spacing = 5;
//    [self.leftView layoutSubviews];
}
- (void)updateRightContentByItem:(YHOptionTableViewItem *)item{
    
    self.rightShowType = item.layout.rightShowType;
    if(item.layout.rightShowType & YHOptionCellRightShowType_Image){
        [self.rightImageView yh_setImage:item.rightContent.imageURL placeholderImage:item.layout.rightImagePlaceholder];
        if(item.layout.rightImageSize.width > 0){
            [self.rightImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(item.layout.rightImageSize);
            }];
            self.rightImageView.layer.cornerRadius = item.layout.rightImageCornerRadius;
            self.rightImageView.layer.masksToBounds = YES;
        }else{
            UIImage * image = [UIImage imageNamed:NullShow(item.rightContent.imageURL)];
            if(image){
                [self.rightImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.size.mas_equalTo(image.size);
                }];
            }else{
                [self.rightImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                }];
            }
        }
    }
    if(item.layout.rightShowType & YHOptionCellRightShowType_Switch){
        [self.rightSwitchView setOn:item.rightContent.isOn animated:YES];
        if(item.layout.rightSwitchDisable){
            self.rightSwitchView.enabled = NO;
            self.rightSwitchView.onTintColor = [UIColor yh_hint];
        }else{
            self.rightSwitchView.enabled = YES;
            self.rightSwitchView.onTintColor = nil;
        }
    }
    if(item.layout.rightShowType & YHOptionCellRightShowType_Title){
        self.rightTitleView.textColor = item.layout.rightTitleColor?:[UIColor yh_h1];
        self.rightTitleView.font = item.layout.rightTitleFont?:[UIFont yh_pfOfSize:12];
        if(item.layout.rightTitleSize.height !=0 &&
           item.layout.rightTitleSize.width != 0){
            self.rightTitleView.textAlignment = NSTextAlignmentCenter;
            self.rightTitleView.layer.cornerRadius = item.layout.rightTitleCornerRadius;
            self.rightTitleView.layer.masksToBounds = YES;
            self.rightTitleView.layer.borderWidth = 0.5;
            self.rightTitleView.layer.borderColor = item.layout.rightTitleColor?item.layout.rightTitleColor.CGColor:[UIColor yh_h1].CGColor;
            [self.rightTitleView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(item.layout.rightTitleSize);
            }];
        }else{
            self.rightTitleView.textAlignment = NSTextAlignmentRight;
            [self.rightTitleView mas_remakeConstraints:^(MASConstraintMaker *make) {
            }];
        }
        
        if(item.rightContent.attibutedTitle){
            self.rightTitleView.attributedText = item.rightContent.attibutedTitle;
        }else{
            self.rightTitleView.text = item.rightContent.title;
        }
    }
    if(item.layout.rightShowType & YHOptionCellRightShowType_Clean){
        [self showCellCleanButtonStatus:item];
    }
}


- (void)showCellCleanButtonStatus:(YHOptionTableViewItem *)item{
    if(item.layout.rightCleanBtnStatus == YHOptionCellCleanStatus_ContentNonEmpty){
        if(IsNull(item.content) &&
           IsNull(item.pickerContents)){
            self.rightCleanView.hidden = YES;
        }else{
            self.rightCleanView.hidden = NO;
        }
    }
    else if(item.layout.rightCleanBtnStatus == YHOptionCellCleanStatus_Always){
        self.rightCleanView.hidden = NO;
    }
}

@end
